﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Odbc;
using System.Collections;

using FoxConn.Lib.Dbo;
using FoxConn.Lib.Winform;

namespace OracleToInformix.Source
{
    class InformixUtil
    {
        public InformixUtil()
        { 
        }
        /// <summary>
        /// 获取Imfomix人事资料中现有数据数
        /// </summary>
        /// <returns>string</returns>
        public static string GetHrmEmployeeCount()
        {
            string strResult = "";
            string strSql = "select count(cpf01) as cou from ecpf_file";

            DBOperation dbo = new DBOperation(1);
            DataSet ds = dbo.GetResult(strSql);

            if(StaticUtil.IsDsHasData(ds))
            {
                strResult = ds.Tables[0].Rows[0]["cou"].ToString();
            }

            return strResult;
        }

        /// <summary>
        /// 根据员工工号查询数据是否存在
        /// </summary>
        /// <param name="WorkNo">员工工号</param>
        /// <returns>bool</returns>
        public static bool IsDataHasCurrentInfo(string WorkNo)
        { 
            bool blnResult = false;
            string strSql = "select cpf01 from ecpf_file where cpf01 = '" + WorkNo + "'";

            DBOperation dbo = new DBOperation(1);
            DataSet ds = dbo.GetResult(strSql);

            if (StaticUtil.IsDsHasData(ds))
            {
                blnResult = true;
            }

            Console.WriteLine(WorkNo + " " + blnResult);
            return blnResult;
        }


        /// <summary>
        /// 删除Infomix中已存在的数据
        /// </summary>
        /// <returns>bool</returns>
        public static bool DeleteOldHrData()
        {
            DBException dbex = new DBException();

            Console.WriteLine("Infromix DataBase have " + GetHrmEmployeeCount() + " records!!!!");
            dbex.SaveErrorLog("Infromix DataBase have " + GetHrmEmployeeCount() + " records!!!!");

            bool blnResult = false;
            DataSet ds = OracleUtil.GetOldHrData();
            ArrayList al = new ArrayList();

            if (StaticUtil.IsDsHasData(ds))
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    //if (IsDataHasCurrentInfo(ds.Tables[0].Rows[i][0].ToString()))
                    //{
                        string strSql = "delete from ecpf_file where cpf01 = '" + ds.Tables[0].Rows[i][0].ToString() + "'";
                        al.Add(strSql);

                        //Console.WriteLine(strSql);
                    //}

                    XMLutil xmlOperator = new XMLutil();
                    if (xmlOperator.GetXMLParameter("Debug") != "0")
                    {
                        if (i % 20 == 0)
                        {
                            Console.Clear();
                        }
                    }
                }

                if (al != null && al.Count > 0)
                {
                    Console.WriteLine("Deleting old data,please wait....");
                    dbex.SaveErrorLog("Deleting old data,please wait....");

                    OdbcDBOpration odbcDBOpration = new OdbcDBOpration();
                    blnResult = odbcDBOpration.ExecuteTransaction(al);

                    if (blnResult)
                    {
                        Console.WriteLine("Delete old data success!!!!");
                        dbex.SaveErrorLog("Delete old data success!!!!");
                    }
                    else
                    {
                        Console.WriteLine("Delete old data failed!!!!");
                        dbex.SaveErrorLog("Delete old data failed!!!!");
                    }
                    Console.WriteLine("Infromix DataBase have " + GetHrmEmployeeCount() + " records now!");
                    dbex.SaveErrorLog("Infromix DataBase have " + GetHrmEmployeeCount() + " records now!");
                }
            }

            return blnResult;
        }

        /// <summary>
        /// 更新个别特殊资料信息
        /// </summary>
        /// <returns>bool</returns>
        public static bool UpdateSpecialInfo()
        {
            string strSql = "update ecpf_file set cpf37 = 'NL2615' where cpf37 in ('NL2625' ,'NL2635','NL2645')";

            DBOperation dbo = new DBOperation(1);
            return dbo.UpdateData(strSql);
        }
    }
}
